<template>
  <div class="login">
    <!--登录区域表单-->
    <div class="form-box">
      <div class="box-title">账号登录</div>
      <el-form ref="formRef" :model="formData" :rules="rules">
        <el-form-item prop="username">
          <template #label>
            <div class="form-item-label">
              <el-icon size="20"><User /></el-icon>
            </div>
          </template>
          <bs-input v-model="formData.username" placeholder="请输入账号"></bs-input>
        </el-form-item>
        <el-form-item prop="password">
          <template #label>
            <div class="form-item-label">
              <el-icon size="20"><Lock /></el-icon>
            </div>
          </template>
          <bs-input type="password" v-model="formData.password" placeholder="请输入密码"></bs-input>
        </el-form-item>
        <!-- <el-form-item prop="captchaValue">
          <template #label>
            <div class="form-item-label">
              <el-icon size="20"><SvgIcon name="identy-code"/></el-icon>
            </div>
          </template>
          <div class="identy-code-content">
            <div class="input-wrap">
              <bs-input v-model="formData.captchaValue" placeholder="请输入验证码"></bs-input>
            </div>
            <div class="code" @click="handleCapt()">
              <img :src="captImg">
            </div>
          </div>
        </el-form-item> -->
        <el-form-item>
          <el-button class="block-btn" size="large" type="primary" @click="handleLogin()">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script lang="ts" setup>
// import SvgIcon from '@/components/SvgIcon.vue'
import Jsencrypt from 'jsencrypt'
import {ref} from 'vue'
import {PUBLIC_KEY} from '@/data'
import {useRoute, useRouter} from 'vue-router'
import useUserStore from '@/store/user'

const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const jsencrypt = new Jsencrypt()
jsencrypt.setKey(PUBLIC_KEY)
const formRef = ref()
const formData = ref({
  username: '',
  password: '',
})
const rules = {
  username: [{
    required: true,
    message: '请输入账号'
  }],
  password: [{
    required: true,
    message: '请输入密码'
  }],
  captchaValue: [{
    required: true,
    message: '请输入验证码'
  }]
}
// const captImg = ref('')

// const captcha = async () => {
//   const res:any = await getCaptcha()
//   const data = res.data
//   formData.value.captchaKey = data.key
//   captImg.value = data.image_base
// }

// const handleCapt = () => {
//   captcha()
// }

const handleLogin = async () => {
  await formRef.value.validate()
  await userStore.login({
    ...formData.value,
    // password: jsencrypt.encrypt(formData.value.password),
    // captchaValue: Number(formData.value.captchaValue)
  })
  const refer = (route.query['refer'] || '/') as string
  router.push(refer)
}

// captcha()

</script>
<style lang="scss" scoped>
.login {
  background-color: #005CE6;
  background-image: linear-gradient(to bottom, #005CE6 0, #96befa 100%);
  height: 100vh;
}
.form-box {
  border-radius: 8px;
  width: 420px;
  // height: 400px;
  background-color: #fff;
  left: 50%;
  box-shadow: 0 2px 13px 0 rgb(0, 0, 0);
  position: fixed;
  z-index: 10;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 16px;
  :deep(.el-form-item.is-required:not(.is-no-asterisk).asterisk-left > .el-form-item__label:before) {
    font-size: 0;
    margin-right: 0;
  }
  .box-title {
    text-align: center;
    font-size: 24px;
    margin-bottom: 16px;
  }
  .form-item-label {
    line-height: 48px;
  }
  .identy-code-content {
    display: flex;
    width: 100%;
    .input-wrap {
      flex: 1;
    }
    .code {
      width: 120px;
      height: 40px;
      margin-left: 10px;
      background-color: #96befa;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .block-btn {
    width: 100%;
  }
}
</style>
